import 'package:fish_redux/fish_redux.dart';
import 'package:www.znpigai.teacher/home_page/subject_tab_component/item/component.dart';
import 'package:www.znpigai.teacher/home_page/subject_tab_component/item/state.dart';
import 'package:www.znpigai.teacher/home_page/subject_tab_component/list/state.dart';

import 'reducer.dart';

class SubjectAdapter extends DynamicFlowAdapter<SubjectListState> {
  SubjectAdapter()
      : super(
          pool: <String, Component<Object>>{
            "item": SubjectComponent(),
          },
          connector: _SubjectConnector(),
          reducer: buildReducer(),
        );
}

class _SubjectConnector extends ConnOp<SubjectListState, List<ItemBean>> {
  @override
  List<ItemBean> get(SubjectListState state) {
    if (state.items?.isNotEmpty == true) {
      return state.items
          .map<ItemBean>((SubjectState data) => ItemBean('item', data))
          .toList(growable: true);
    } else {
      return <ItemBean>[];
    }
  }

  @override
  void set(SubjectListState state, List<ItemBean> items) {
    if (items?.isNotEmpty == true) {
      state.items = List<SubjectState>.from(
          items.map<SubjectState>((ItemBean bean) => bean.data).toList());
    } else {
      state.items = <SubjectState>[];
    }
  }

  @override
  subReducer(reducer) {
    return super.subReducer(reducer);
  }
}
